package com.v5.ik;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.gonghui.videomanage.VideoManage;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zy on 2016/9/26.
 */
public class PYQVideoAdapter extends BaseAdapter {
    private FragmentActivity activity;
    private List<PYQSource> pyqSources;
    final LikeWechatDialog likeWechatDialog;
    private IWXAPI mWXAPI;

    public PYQVideoAdapter(FragmentActivity activity, List<PYQSource> pyqSources) {
        mWXAPI = WXAPIFactory.createWXAPI(activity, Constants.APP_ID, false);
        mWXAPI.registerApp(Constants.APP_ID);
        this.activity = activity;
        likeWechatDialog = new LikeWechatDialog(activity, "请稍等...");
        this.pyqSources = pyqSources;
    }

    @Override
    public int getCount() {
        return pyqSources == null ? 0 : pyqSources.size();
    }

    @Override
    public PYQSource getItem(int i) {
        return pyqSources.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(viewGroup.getContext(), R.layout.pyq_video_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final PYQSource pyqSource = getItem(i);
        viewHolder.tv_name.setText(pyqSource.getName());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date(((long)Integer.valueOf(pyqSource.getTime()))*1000));
        viewHolder.tv_time.setText(dateString);
        viewHolder.video_root.removeAllViews();
        viewHolder.video_root.setVisibility(View.GONE);
        final FrameLayout videoRoot = viewHolder.video_root;
        viewHolder.video_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                notifyDataSetChanged();
                videoRoot.setVisibility(View.VISIBLE);
                VideoManage.getInstance().setRoot(videoRoot).setPath(pyqSource.getPicArr()).prepare();
            }
        });

        viewHolder.tv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareToWeChat(2,pyqSource.getPicArr());
            }
        });
        viewHolder.tv_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareToWeChat(1,pyqSource.getPicArr());
            }
        });

        return view;
    }

    class ViewHolder {
        TextView tv_name,tv_time,tv_share,tv_friend;
        FrameLayout video_root;
        View video_play;

        public ViewHolder(View view) {
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_share = (TextView) view.findViewById(R.id.tv_share);
            tv_friend = (TextView) view.findViewById(R.id.tv_friend);
            video_root = (FrameLayout) view.findViewById(R.id.video_root);
            video_play = view.findViewById(R.id.video_play);
        }
    }

    public void shareToWeChat(int scene,String path) {
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        switch (scene) {
            case 1: //微信好友
                req.scene = SendMessageToWX.Req.WXSceneSession;
                break;
            case 2: //朋友圈
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
                break;
        }

        WXVideoObject video = new WXVideoObject();
        video.videoUrl = path;

        WXMediaMessage message = new WXMediaMessage(video);
//        message.title = title;
//        message.description = summary;

        req.message = message;
        mWXAPI.sendReq(req);
    }
}
